﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Collections.ObjectModel;
using System.IO;

namespace MavisWPFClient
{
    /// <summary>
    /// Interaction logic for MediaPlayer.xaml
    /// </summary>
    public partial class MediaPlayer : Window
    {
        
        public MediaPlayer()
        {
            InitializeComponent();
            
        }

        private void PlayBtn_Click(object sender, RoutedEventArgs e)
        {
            string address = "http://st.sandblu.msn-int.com/br/demo/sample.wmv";
            TeddyPlayer.URL = address;
            string status = TeddyPlayer.status;
            //TeddyPlayer.openPlayer(address);
        }

        private void StopBtn_Click(object sender, RoutedEventArgs e)
        {
            string address = "1.smi";
            
            _textCollection = LoadSAMIFileToClass(address);
            CaptionListView.ItemsSource = TextCollection;
        }

        public static ObservableCollection<SAMI> LoadSAMIFileToClass(string filename)
        {
            List<string> timeList = new List<string>();
            List<string> textList = new List<string>();
            ObservableCollection<SAMI> smiList = new ObservableCollection<SAMI>();
            
            StreamReader reader = new StreamReader(filename);
            string sLine = "";

            while (sLine != null)
            {
                string time = "";
                string text = "";
                sLine = reader.ReadLine();
                if (sLine != null)
                {
                    if (sLine.Contains("SYNC Start="))
                    {
                        time = sLine.Split('=')[1].Split('>')[0];
                        timeList.Add(time);
                    }
                    if (sLine.Contains("Class=ENUSCC") && !sLine.Contains("ID="))
                    {
                        text = sLine.Split('>')[1];
                        textList.Add(text);
                    }
                }
            }

            if (timeList.Count == textList.Count)
            {
                for (int i = 0; i < timeList.Count; i++)
                {
                    SAMI smi = new SAMI();
                    smi.Text = textList[i];
                    smi.Timeline = timeList[i];
                    smiList.Add(smi);
                }
            }
            reader.Close();
            return smiList;
        }

        public class SAMI
        {
            public string Timeline { set; get; }
            public string Text { set; get; }
        }
        ObservableCollection<SAMI> _textCollection = new ObservableCollection<SAMI>();
        public ObservableCollection<SAMI> TextCollection
        {
            get
            {
                return _textCollection;
            }
        }

    }

    
    
}
